package com.chris.homecare.components.menu
{
	import com.chris.homecare.AppScreen;
	import com.chris.homecare.utils.CommonUtils;
	
	import flash.geom.Point;
	
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.layout.HorizontalLayout;
	
	import starling.display.Image;
	import starling.display.Quad;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	[Event(name="closeSubMenuItems",type="starling.events.Event")]
	[Event(name="showSubMenuItems",type="starling.events.Event")]
	public class MenuListComponent extends LayoutGroup
	{
		public static const CLOSE_SUBMENU_ITEMS:String = "closeSubMenuItems";
		public static const SHOW_SUBMENU_ITEMS:String = "showSubMenuItems";
		
		public var givenWidth:Number = 0;
		public var givenHeight:Number = 0;
		private var color:uint;

		private var backgroundQuad:Quad;

		private var contentGroup:LayoutGroup;
		private var headingLabel:Label;
		private var page:String;
		
		protected var _touchPointID:int = -1;
		private static const HELPER_TOUCHES_VECTOR:Vector.<Touch> = new <Touch>[];
		private static const HELPER_POINT:Point = new Point();
		
		private var subMenuObj:Object;
		private var mainMenuObj:Object;
		private var isSubMenu:Boolean = false;
		
		public static var isSubMenuOpened:Boolean = false;

		private var iconImg:Image;
		
		public function MenuListComponent(content:Object, isSubMenu:Boolean=false)
		{
			super();
			
			this.isSubMenu = isSubMenu;
			
			if(!isSubMenu) this.mainMenuObj = content;
			else this.subMenuObj = content;
			
		}
		
		override protected function initialize():void
		{
			super.initialize();
			
			backgroundQuad = new Quad(1,1,0xFFFFFF);
			addChild(backgroundQuad);
			
			contentGroup = new LayoutGroup();
			var contentLayout:HorizontalLayout = new HorizontalLayout();
			contentLayout.paddingLeft = 20*CommonUtils.scaleValue;
			contentLayout.verticalAlign = HorizontalLayout.VERTICAL_ALIGN_MIDDLE;
			contentLayout.gap = 15*CommonUtils.scaleValue;
			contentGroup.layout = contentLayout;
			addChild(contentGroup);
			
			if(isSubMenu)
			{
				this.headingLabel = new Label();
				this.headingLabel.text = this.page = subMenuObj.page;
				this.headingLabel.nameList.add("grouplist-head-label");
				contentGroup.addChild(this.headingLabel);
				this.backgroundQuad.color = subMenuObj.color;
				this.backgroundQuad.width = stage.stageWidth;
				this.height = this.backgroundQuad.height = 80*CommonUtils.scaleValue;
			}
			
			else
			{
				backgroundQuad.color = mainMenuObj.color;
				
				iconImg = mainMenuObj.img;
				addChild(iconImg);
				
				headingLabel = new Label();
				headingLabel.text = mainMenuObj.heading;
				headingLabel.nameList.add("grouplist-head-label");
				contentGroup.addChild(headingLabel);
				
				this.page = mainMenuObj.page;
			}
			
			this.addEventListener(TouchEvent.TOUCH, touchHandler);
		}
		
		override protected function draw():void
		{
			super.draw();
			this.width = backgroundQuad.width = givenWidth;
			this.height = backgroundQuad.height = givenHeight;
			contentGroup.y = (this.height - contentGroup.height)/2;
			
			if(iconImg && givenHeight != 0)
			{
				var imgHeight:Number = (givenHeight*1.6)/3;
				var ratio:Number = imgHeight/iconImg.texture.nativeHeight;
				
				iconImg.height = (givenHeight*1.6)/3;
				iconImg.width = ratio*iconImg.texture.nativeWidth;
				
				iconImg.x = 30*CommonUtils.scaleValue;
				iconImg.y = (givenHeight - iconImg.height)/2;
				contentGroup.x = iconImg.width+iconImg.x + 10*CommonUtils.scaleValue;
			}
		}
		
		protected function touchHandler(event:TouchEvent):void
		{
			if(!this._isEnabled)
			{
				return;
			}
			
			const touches:Vector.<Touch> = event.getTouches(this, null, HELPER_TOUCHES_VECTOR);
			if(touches.length == 0)
			{
				//end of hover
				backgroundQuad.alpha = 1;
				return;
			}
			if(this._touchPointID >= 0)
			{
				var touch:Touch;
				for each(var currentTouch:Touch in touches)
				{
					if(currentTouch.id == this._touchPointID)
					{
						touch = currentTouch;
						break;
					}
				}
				
				if(!touch)
				{
					//end of hover
					backgroundQuad.alpha = 1;
					HELPER_TOUCHES_VECTOR.length = 0;
					return;
				}
				
				if(touch.phase == TouchPhase.ENDED)
				{
					this._touchPointID = -1;
					touch.getLocation(this, HELPER_POINT);
					var isInBounds:Boolean = this.hitTest(HELPER_POINT, true) != null;
					if(isInBounds)
					{
						if(this.page != "Info")
						{
							this.dispatchEventWith(MenuListComponent.CLOSE_SUBMENU_ITEMS, true);
							this.dispatchEventWith(Event.COMPLETE,true,{page:this.page});
							AppScreen.makeGestureAvailable();
							isSubMenuOpened = false;
						}
						
						else
						{
							if(!isSubMenuOpened) 
							{
								this.dispatchEventWith(MenuListComponent.SHOW_SUBMENU_ITEMS, true);
								isSubMenuOpened = true;
							}
							else 
							{
								this.dispatchEventWith(MenuListComponent.CLOSE_SUBMENU_ITEMS, true);
								isSubMenuOpened = false;
							}
						}
					}
					backgroundQuad.alpha = 1;
				}
			}
			else //if we get here, we don't have a saved touch ID yet
			{
				for each(touch in touches)
				{
					if(touch.phase == TouchPhase.BEGAN)
					{
						backgroundQuad.alpha = 0.75;
						this._touchPointID = touch.id;
						break;
					}
				}
			}
			HELPER_TOUCHES_VECTOR.length = 0;
		}
		
	}
}